<?php

require './../librairies/classautoloader/1.0/ClassAutoLoader.php';
//require './scripts/constantes.php';
require "./bdd/bdd.php";
require "../librairies/classe_bdd/1.0.1/classe_bdd.php";


$g_BDD = new classe_bdd(HOST, USER, PASSWD, DATABASE);
if (!$g_BDD->connect()) {
	$l_page = 'err_bdd';
}


$l_requete = 'SELECT * from comptes';

$l_result = $g_BDD->requeteBDD($l_requete);
while($l_compte = $g_BDD->fetchArray($l_result)) {
	echo '-----------------------------------------------------------<br> compte:'.$l_compte['numeroCompte'].'<br>';
	echo '-----------------------------------------------------------<br>';
	genereLignes($l_compte['numeroCompte']);
}










function genereLignes($p_compte) {
	global $g_BDD;
	$l_requete = "SELECT * FROM `$p_compte`";
	$l_result = $g_BDD->requeteBDD($l_requete);
	while($l_compte = $g_BDD->fetchArray($l_result)) {
		
			$ligne='';
			if($l_compte['numeroCompteOri']=='') {
				$ligne='INSERT INTO operation (`noCompte`, `noReleve`, `date`, `libelle`, `fluxId`, `modePaiementId`, `montant`, `verif`, `numeroCompteOri`, `operationIdOri`) 
					VALUES
					(\''.$p_compte .'\', '.
					$l_compte['noReleve'] .', \''.
					$l_compte['date'] .'\', \''.
					mysql_real_escape_string($l_compte['libelle']) .'\', '.
					$l_compte['fluxId'] .', '.
					$l_compte['modePaiementId'] .', \''.
					$l_compte['montant'] .'\', \''.
					$l_compte['verif'] .'\', \''.
					$l_compte['numeroCompteOri'] .'\', '.
					$l_compte['operationId'] .')';
			} else {
				echo "-------------------------------<br>";
				echo "operation: ".$l_compte['operationId'].'<br>';
				$origine = rechercheOrigine($l_compte['numeroCompteOri'], $l_compte['operationIdOri']);
				echo "origine:$origine <br>";
				$ligne='INSERT INTO operation (`noCompte`, `noReleve`, `date`, `libelle`, `fluxId`, `modePaiementId`, `montant`, `verif`, `numeroCompteOri`, `operationIdOri`) 
					VALUES
					(\''.$p_compte .'\', '.
					$l_compte['noReleve'] .', \''.
					$l_compte['date'] .'\', \''.
					mysql_real_escape_string($l_compte['libelle']) .'\', '.
					$l_compte['fluxId'] .', '.
					$l_compte['modePaiementId'] .', \''.
					$l_compte['montant'] .'\', \''.
					$l_compte['verif'] .'\', \''.
					$l_compte['numeroCompteOri'] .'\', '.
					$origine .')';
				
			}
			if($g_BDD->requeteBDD($ligne)!= true) {
				echo $ligne."<br>";
				echo $g_BDD->getLastError();
				exit(0);
			}
		
	}
}


function rechercheOrigine($p_numeroCompte, $p_numeroOperation) {
	global $g_BDD;
	$l_requete="SELECT * FROM operation WHERE noCompte='$p_numeroCompte' and operationIdOri=$p_numeroOperation";
	echo $l_requete.'<br>';
	$l_result = $g_BDD->requeteBDD($l_requete);
	echo 'nb'.$g_BDD->nbRows($l_result).'<br>';
	if($l_result==null) {
		echo $ligne."<br>";
		echo $g_BDD->getLastError();
		exit(0);
	}
	$l_operation = $g_BDD->fetchArray($l_result);
	echo 'operation libelle: '.$l_operation['noCompte'].'<br>';
	return $l_operation['operationId'];
}

?>